package com.lizhi.yun.core.heart.task;

import com.lizhi.common.yun.enums.UppSystemEnum;
import com.lizhi.yun.db.dao.UppSystemDao;
import com.lizhi.yun.db.pojo.UppSystemPOJO;
import com.lizhi.utils.date.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/7/7 9:37
 */
@Slf4j
@Component  //用于对那些比较中立的类进行注释；
@EnableScheduling // 开启定时任务
@EnableAsync  //开启多线程
public class HeartScheduleTask {

    @Resource
    private UppSystemDao uppSystemDao;

    @Async
    @Scheduled(cron = "0/30 * * * * ?")
    public void uploadDateTick(){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("systemStatus", UppSystemEnum.SYSTEM_STATUS_00.getEnValue());
        List<UppSystemPOJO> lists = uppSystemDao.selectByMap(paramMap, null);
        for (int i = 0; i < lists.size(); i++){
            UppSystemPOJO uppSystemPOJO = lists.get(i);
            log.debug("上次心跳时间uppSystemPOJO.getHeartTm():{}", uppSystemPOJO.getHeartTm());
            if (DateUtils.checkRpcDate(uppSystemPOJO.getHeartTm(), 90000)){
                uppSystemPOJO.setSystemStatus(UppSystemEnum.SYSTEM_STATUS_01.getEnValue());
                uppSystemDao.updateSystemStatus(uppSystemPOJO);
            }
        }
    }
}
